package org.example.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.example.service.ShoppingCartService;
import org.example.util.JsonResult;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/cart")
public class ShoppingCartController {
    @Resource
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add/{id}")
    public JsonResult add(@PathVariable("id") String bookId, HttpServletRequest request) {
        String token = request.getHeader("token");
        return shoppingCartService.add(bookId, token);
    }
    @GetMapping("/find")
    public JsonResult find(HttpServletRequest request) {
        return shoppingCartService.find(request.getHeader("token"));
    }
    @PutMapping("/chang_cart_number/{bookId}/{number}")
    public JsonResult changCartNumber(@PathVariable("bookId") String bookId, @PathVariable("number") int number, HttpServletRequest request) {
        String token = request.getHeader("token");
        return shoppingCartService.changCartNumber(token, bookId, number);
    }

    @PutMapping("/chang_cart_number2/{bookId}/{number}")
    public JsonResult changCartNumber2(@PathVariable("bookId") String bookId, @PathVariable("number") int number, HttpServletRequest request) {
        String token = request.getHeader("token");
        return shoppingCartService.changCartNumber2(token, bookId, number);
    }
    @DeleteMapping("/remove/{bookId}")
    public JsonResult remove(@PathVariable("bookId") String bookId, HttpServletRequest request) {
        String token = request.getHeader("token");
        return shoppingCartService.removeCartById(token, bookId);
    }

    @DeleteMapping("/batch_remove")
    public JsonResult batchRemoveCart(@RequestParam("ids") String[] ids, HttpServletRequest request) {
        String token = request.getHeader("token");
        return shoppingCartService.batchRemoveCart(token, ids);
    }

}
